package com.example.deploysinggle.test1.common.config;

import com.example.deploysinggle.test1.common.filter.CommonFilter;
import com.example.deploysinggle.test1.common.filter.IPFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/12/15 19:19
 */
@Configuration
public class FilterConfig {

    @Autowired
    private IPFilter ipFilter;

    @Bean
    public FilterRegistrationBean<CommonFilter> commonFilterFilterRegistrationBean() {
        FilterRegistrationBean<CommonFilter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(new CommonFilter());
        registrationBean.addUrlPatterns("/*"); // 过滤所有请求
        registrationBean.setOrder(1); // 设置过滤器的顺序

        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<IPFilter> ipFilterFilterRegistrationBean() {
        FilterRegistrationBean<IPFilter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(ipFilter);
        registrationBean.addUrlPatterns("/*"); // 过滤所有请求
        registrationBean.setOrder(2); // 设置过滤器的顺序

        return registrationBean;
    }
}
